﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor
{
    public partial class KoliUstuCC : DevExpress.XtraReports.UI.XtraReport
    {
        DataTable dtBeden;
        public KoliUstuCC(DataTable dt, bool PDF, string Sezon, bool IsKarbelFooter)
        {
            InitializeComponent();

            dtBeden = dt;
            xrLabel3.HeightF = xrLabel4.HeightF = 1;

            if (Sezon == string.Empty)
            {
                xrSezon.Dispose();
                xrSezon2.Dispose();
            }
            else
                xrSezon.Text = xrSezon2.Text = Sezon;
            xrKarbel.Visible = xrKarbel2.Visible = IsKarbelFooter;

            MakeResizable(xrClents, xrClents2);

            myData1.K_37.Merge(dt);

            //if (PDF)
            //{
            //    string fileName = Dialogs.SaveFile("PDF Document", "PDF Files|*.pdf");
            //    this.ExportToPdf(fileName);
            //}

            //string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //if (PDF)
            //    this.ExportToPdf(path+"\\"+Tur+".pdf");
            /*
            string style = string.Empty;
            if (PDF)
                do
                {
                    style = myData1.K_S.Compute("MIN(STYLE)", "STYLE>'" + style + "'").ToString();
                    if (style == string.Empty)
                        break;

                    this.FilterString = "STYLE='" + style + "'";

                    //Dim report As DevExpress.XtraReports.UI.XtraReport = XtraReport.FromFile(fileName, True)

                    //report.DataSource = ds
                    //report.DataMember = "ReportData"

                    this.ExportToPdf(@"D:\reports\37\" + style + ".pdf");
                }
                while (true);
            */
        }


        #region MakeResizable
        Dictionary<string, Font> defaultFonts = new Dictionary<string, Font>();
        void MakeResizable(params XRControl[] controls)
        {
            foreach (XRTableCell cell in controls)
            {
                defaultFonts.Add(cell.Name, cell.Font);
                cell.BeforePrint += new PrintEventHandler(cell_BeforePrint);
            }
        }

        void cell_BeforePrint(object sender, PrintEventArgs e)
        {
            Resize((XRControl)sender);
        }

        private void Resize(XRControl cell)
        {
            //Resize ı table'larda düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            float factor = 10;

            Font defaultFont = defaultFonts[cell.Name];
            float newSize = defaultFont.Size;

            float stringWidth = gr.MeasureString(cell.Text, defaultFont).Width;
            float cellWidth = cell.WidthF;

            if (cellWidth < stringWidth * factor)
                newSize = defaultFont.Size * cell.Width / stringWidth / factor;

            Font newFont = new Font(defaultFont.Name, newSize, defaultFont.Style, defaultFont.Unit, defaultFont.GdiCharSet, defaultFont.GdiVerticalFont);

            cell.Font = newFont;

            gr.Dispose();
        }
        #endregion

        private void Detail_BeforePrint(object sender, PrintEventArgs e)
        {
            XRTable[] tables = new XRTable[] { tbBaslik, tbBaslik2, tbDetay, tbDetay2 };

            foreach (XRTable table in tables)
            {
                for (int i = table.Rows.Count - 1; i >= 0; i--)
                {
                    table.Rows[i].Dispose();
                }
            }

            int RowNr = GetCurrentColumnValue("RowNr").ToInt();
            string strModelDetay = "";

            foreach (DataColumn dc in dtBeden.Columns)
            {
                string activeColName = dc.ColumnName;
                if (activeColName.StartsWith("F"))
                {
                    if (dtBeden.Select(string.Format("RowNr={0} and Isnull({1},0)>0", RowNr, activeColName)).Length > 0)
                    {
                        strModelDetay += activeColName + ";";
                    }
                }
            }

            int ColumnCount = strModelDetay.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length;
            decimal ColumnWidth = decimal.Round(1315 / ColumnCount, 2);

            foreach (XRTable table in tables)
            {
                if (table.Name.Contains("Detay"))
                    continue;

                XRTableRow trBaslik = new XRTableRow();
                foreach (string colName in strModelDetay.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    trBaslik.HeightF = 151.91F;

                    XRTableCell tcBaslik = new XRTableCell();

                    tcBaslik.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Right) | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tcBaslik.Font = new System.Drawing.Font("Calibri", 21F, System.Drawing.FontStyle.Bold);
                    tcBaslik.Text = colName.Substring(1, colName.Length - 1);
                    tcBaslik.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    tcBaslik.WidthF = (float)ColumnWidth;

                    trBaslik.Cells.AddRange(new XRTableCell[] { tcBaslik });
                }
                table.Rows.Add(trBaslik);
            }


            foreach (XRTable table in tables)
            {
                if (table.Name.Contains("Baslik"))
                    continue;

                XRTableRow trDetay = new XRTableRow();
                DataRow drBeden = dtBeden.Select("RowNr=" + RowNr)[0];
                foreach (string colName in strModelDetay.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    XRTableCell tcDetay = new XRTableCell();

                    tcDetay.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Right) | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tcDetay.Text = drBeden[colName].ToString();
                    tcDetay.Font = new System.Drawing.Font("Calibri", 22F);
                    tcDetay.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    tcDetay.WidthF = (float)ColumnWidth;

                    trDetay.HeightF = 121.91F;
                    trDetay.Cells.AddRange(new XRTableCell[] { tcDetay });
                }
                table.Rows.Add(trDetay);
            }
            /*
            string client = GetCurrentColumnValue("CLENTS").ToString();
            if (client.ToLower().Contains("vente") || true)
            {
                xrTableCell1.Visible = xrTableCell79.Visible = false;
                xrTableCell4.Text = xrTableCell13.Text = "STOCK ORDER";//6 ALLEE DES CHÊNES \nPARC INDUSTRIEL DE LA PLAINE DE L'AIN \nBATIMENT  B  01150 SAINT VULBAS,  
                    
            }
            else
            {
                xrTableCell1.Visible = xrTableCell79.Visible = true;
                xrTableCell4.Text = xrTableCell13.Text = "";
            }
    */
        }

        private void xrLabel3_BeforePrint(object sender, PrintEventArgs e)
        {
            float f = xrLabel3.HeightF;
        }

        private void xrLabel4_BeforePrint(object sender, PrintEventArgs e)
        {
            float f = xrLabel3.HeightF;
        }
    }
}